package br.ufrj.dcc.mapaviolencia.util;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import br.ufrj.dcc.mapaviolencia.job.TwitterJob;

public class QuartzUtil {

	public static String JOB_TWITTERJOB = "TwitterJob";
	public static String TRIGGER_TWITTERJOB = "TwitterJobTrigger";
	
	public static void initialize() throws SchedulerException {
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.start();
	}
	
	public static void shutdown() throws SchedulerException {
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.shutdown(true);
	}
	
	public static void scheduleTwitterJob() throws SchedulerException {
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        JobDetail job = newJob(TwitterJob.class).withIdentity(JOB_TWITTERJOB).build();
        Trigger trigger = newTrigger().withIdentity(TRIGGER_TWITTERJOB).startNow()
        		.withSchedule(simpleSchedule().withIntervalInMinutes(1).repeatForever()).build();
        scheduler.scheduleJob(job, trigger);
	}
	
}
